package mpcs.model.vo
{
	import flash.events.Event;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	import mpcs.ApplicationFacade;
	import mpcs.NotiConst;
	import mpcs.utils.xml.XMLUtils;

	/**
	 *  存放全局数据
	 * @author zhangzuoqiang
	 */
	public class GlobalVO {
		
		public static var mpcsApp:MainApp;
		
		// 登陆界面的配置文件
		private static var preferences:XML;
		
		/** 用户是否已经登录 **/
		public static var isLogin:Boolean = false;
		
		[Embed(source="assets/logo/logo_24.png")]
		public static var logoIcon:Class;
		
		/**存放全局用户信息**/
		[Bindable] public static var userVO:UserVO = new UserVO();
		
		public static function getPreferences():XML {
			return preferences;
		}
		
		/**
		 * 初始化登陆界面的配置文件
		 */
		public static function initPreferencesXML():void {
//			preferences = new XML();
//			var xmlLoader:URLLoader = new URLLoader();
//			xmlLoader.load(new URLRequest('data/preferences.xml'));
//			xmlLoader.addEventListener(Event.COMPLETE, processXML);
			preferences = XMLUtils.loadXMLFile( "data/preferences.xml" );
			processXML();
		}
		
//		private static function processXML(e:Event):void {
//			preferences = new XML(e.target.data);	
		private static function processXML():void {
			var remember:String = preferences.checkbox.pwd.@isremember ;
			mpcsApp.loginForm.email.text = preferences.users.user.@email ;
			if(remember == "true"){
				mpcsApp.loginForm.remember.selected = true;
				mpcsApp.loginForm.password.text = preferences.users.user.@password ;
			}
			var autologin:String = preferences.checkbox.autologin.@isremember ;
			if(autologin == "true"){
				mpcsApp.loginForm.autologin.selected = true;
				trace("auto login is selected ... ");
				autoLogin();
			}
		}
		
		private static function autoLogin():void {
			var uservo:UserVO = new UserVO(mpcsApp.loginForm.email.text, mpcsApp.loginForm.password.text);
			ApplicationFacade.getInstance().sendNotification( NotiConst.S_LOGIN, uservo);
			mpcsApp.loginForm.loginBtn.enabled = false;
		}
		
	}
}